package controller.admin;

import model.Page;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet(name = "AdminOrderListServlet", value = "/admin/order_list")
public class AdminOrderListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        OrderService os = new OrderService();
        //初始化参数
        int pageNumber = 1;
        int status = 0;

        //获取参数
        String tempPageNumber = request.getParameter("pageNumber");
        String tempStatus = request.getParameter("status");

        //如果传了就赋值
        if (tempPageNumber != null && !"".equals(tempPageNumber)) {
            //防止页码小于1
            pageNumber = Math.max(Integer.parseInt(tempPageNumber), 1);
        }
        //如果传了就赋值
        if (tempStatus != null && !"".equals(tempStatus)) {
            status = Math.max(Integer.parseInt(tempStatus), 0);
        }

        try {
            Page page = os.selectPageByStatus(status, pageNumber);
            request.setAttribute("p", page);
            request.setAttribute("status", status);
            request.getRequestDispatcher("order_list.jsp").forward(request, response);
        } catch (SQLException e) {
            request.getRequestDispatcher("order_list.jsp").forward(request, response);
            e.printStackTrace();
        }

    }
}
